﻿#region << 文件说明 >>
/*----------------------------------------------------------------
 * 命名空间：Vampirewal.Core.Tools
 * 唯一标识：8d8f3648-93de-47c8-a501-640c9fe04b29
 * 文件名：ZipHelper
 * 
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2025/6/4 22:13:36
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 文件说明 >>


namespace Vampirewal.Core;

/// <summary>
/// 使用Base64压缩
/// </summary>
public static class Base64ZipHelper
{
    /// <summary>
    /// 将字符串压缩成Base64
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string ZipAndBase64Encode(string input)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(input);
        MemoryStream inputms = new MemoryStream(buffer);
        MemoryStream outputms = new MemoryStream();
        using (GZipStream zip = new GZipStream(outputms, CompressionMode.Compress))
        {
            inputms.CopyTo(zip);
        }
        byte[] rv = outputms.ToArray();
        inputms.Dispose();
        return Convert.ToBase64String(rv);
    }

    /// <summary>
    /// 将Base64字符串解压缩
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string UnZipAndBase64Decode(string input)
    {
        byte[] inputstr = Convert.FromBase64String(input);
        MemoryStream inputms = new MemoryStream(inputstr);
        MemoryStream outputms = new MemoryStream();
        using (GZipStream zip = new GZipStream(inputms, CompressionMode.Decompress))
        {
            zip.CopyTo(outputms);
        }
        byte[] rv = outputms.ToArray();
        outputms.Dispose();
        return Encoding.UTF8.GetString(rv);
    }
}